package web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import objects.Board;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

/**
 * Servlet implementation class ContradictCard
 */
@WebServlet(name = "ContradictCard", urlPatterns =
{
    "/ContradictCard"
})
public class ContradictCardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ContradictCardServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("application/json");
	    response.setCharacterEncoding("UTF-8");
		
		String src = request.getParameter("contradictCard");
		String contradictPlayer = request.getParameter("contradictPlayer");
		String currPlayer = Board.getInstance().getCurrPlayerInstance().getPlayerName();
		
		
		String imgTag = "<img src= " + src + " width=\"140px\" height=\"140px\"/>\r\n";
		
		// Send HTML content of the contradicting cards.
		String content = "<td align=\"center\" width=\"100%\" colspan=\"3\">\r\n" + 
						"					<table>\r\n" + 
						"						<tr>\r\n" + 
						"<td align=\"center\"\r\n" + 
						contradictPlayer + " shows you" + ":\r\n" + 
						"							</td>\r\n" + 
						"						</tr>\r\n" + 
						"<tr> <td> " +  request.getParameter("label") + "</td></tr>" +  
						"						<tr>\r\n" + 
						"							<td>\r\n" + imgTag + 
						"							</td>\r\n" + 
						"						</tr>\r\n" + 
						"						\r\n" + 
						"					</table>\r\n" + 
						"				</td>";
		
		JsonObject jsAction = new JsonObject();
		jsAction.addProperty("code", "contradict");
		jsAction.addProperty("player", currPlayer);
		jsAction.addProperty("content", content);
		jsAction.addProperty("contPlayer", contradictPlayer);
		jsAction.addProperty("message", "Player " +contradictPlayer+ " showed " + currPlayer + " a contradicting card!");
		jsAction.addProperty("playerType", Board.getInstance().getCurrPlayerInstance().getType());
		Board.getInstance().addJsActions(jsAction);
		
		if (Board.getInstance().getCurrPlayerInstance().getType().equals("COMPUTER"))
		{
			/*
			String playerOld = Board.getInstance().getCurrPlayerInstance().getPlayerName();
			Board.getInstance().advanceTurn();
			String playerNew = Board.getInstance().getCurrPlayerInstance().getPlayerName();
			
			JsonObject jsAction2 = new JsonObject();
			jsAction2.addProperty("code", "endTurn");
			jsAction2.addProperty("playerOld", playerOld);
			jsAction2.addProperty("playerNew", playerNew);
			jsAction2.addProperty("message", "Player " +playerOld+ " finished his turn. It's " + playerNew + "'s turn to play.");
			Board.getInstance().addJsActions(jsAction2);
			*/
			EndTurnServlet.turnEnder();
		}
	}
	
	@SuppressWarnings("unused")
	// Inner class used to deserialize json into info
    private class ResultJson {
        private String res;
        private String content;
    }   

}
